/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.annotation.DataFilter;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.dao.SysRoleDao;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.renren.modules.sys.entity.SysDictEntity;
import io.renren.modules.sys.entity.SysRoleEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Mark sunlightcs@gmail.com
 */
public interface SysDictService extends IService<SysDictEntity> {

    PageUtils queryPage(Map<String, Object> params);

    /**
     * 角色
     *
     * @author Mark sunlightcs@gmail.com
     */
    @Service("sysRoleService")
    class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
        @Autowired
        private SysRoleMenuService sysRoleMenuService;
        @Autowired
        private SysRoleDeptService sysRoleDeptService;
        @Autowired
        private SysUserRoleService sysUserRoleService;
        @Autowired
        private SysDeptService sysDeptService;

        @Override
        @DataFilter(subDept = true, user = false)
        public PageUtils queryPage(Map<String, Object> params) {
            String roleName = (String)params.get("roleName");

            IPage<SysRoleEntity> page = this.page(
                new Query<SysRoleEntity>().getPage(params),
                new QueryWrapper<SysRoleEntity>()
                    .like(StringUtils.isNotBlank(roleName),"role_name", roleName)
                    .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
            );

            for(SysRoleEntity sysRoleEntity : page.getRecords()){
                SysDeptEntity sysDeptEntity = sysDeptService.getById(sysRoleEntity.getDeptId());
                if(sysDeptEntity != null){
                    sysRoleEntity.setDeptName(sysDeptEntity.getName());
                }
            }

            return new PageUtils(page);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveRole(SysRoleEntity role) {
            role.setCreateTime(new Date());
            this.save(role);

            //保存角色与菜单关系
            sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

            //保存角色与部门关系
            sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void update(SysRoleEntity role) {
            this.updateById(role);

            //更新角色与菜单关系
            sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

            //保存角色与部门关系
            sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void deleteBatch(Long[] roleIds) {
            //删除角色
            this.removeByIds(Arrays.asList(roleIds));

            //删除角色与菜单关联
            sysRoleMenuService.deleteBatch(roleIds);

            //删除角色与部门关联
            sysRoleDeptService.deleteBatch(roleIds);

            //删除角色与用户关联
            sysUserRoleService.deleteBatch(roleIds);
        }


    }
}

